<?
require("required/dbconnect.php");

require("required/chkuser.php");
require("required/function.php");
require("required/share_function.php");
$title = "Order Status";
require("required/header.php");
$bid = mysql_real_escape_string($_GET['id']);

$get_bill = mysql_query("select * from os_bill_details where id=$bid");
$b = mysql_fetch_array($get_bill, MYSQL_ASSOC);

if ($b['uid'] != $member_id) {
    errRedirect(2, "Invalid Access!", $_SERVER['PHP_SELF'], 0);
}

$get_item = mysql_query("select * from os_bill_items where bid='$bid' and addon_for=0") or die("line 11" . mysql_error());
$count = mysql_num_rows($get_bill);
$title = $b['invoice_number'];


$_SESSION['billing'] = 0;
?>
<table width="100%" cellpadding="6" cellspacing="0">
    <td align="left" width="100%" class="page_title_bar">


        Confirmed order


    </td>

    <tr>
        <Td>
            <table width="100%">
                <?
                if ($count == 0) {
                    ?>
                    <tr>
                        <td align="center"> :: NO RECORD :: </td>
                    </tr>
                    <?
                } else {
                    ?>
                    <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" name="frm">


                        <tr>
                            <td>

                                <table width="100%" cellpadding="0" cellspacing="0">

                                    <tr>
                                        <td >
                                            <table><td>

                                                    <fieldset><legend>Status : <font color="#0066CC"><?= billstatus_eng($b['status']) ?></font></legend>
                                                        <?
                                                        if ($b['status'] <= 2 && $b['status'] > 1) {
                                                            $dayleft = 7 - dayDiff($b['verified_date']);
                                                            $timeStamp = strtotime($b['verified_date']);
                                                            $timeStamp += 24 * 60 * 60 * $dayleft;
                                                            $expiryDate = date("Y-m-d", $timeStamp);
                                                            ?>
                                                            <font class="page_description">
                                                            An invoice has been sent to <strong><?= $b['email'] ?></strong>.<br>
                                                            Please make payment within <font color=red><?= $dayleft ?></font> day(s), the expiry date for this order is on <font color=red><?= $expiryDate ?></font><br>
                                                            </font>

                                                            <br>Kindly make payment to the following:
                                                            <?
                                                            $get_bank = mysql_query("select * from os_bankacc where id=1");

                                                            $bank = mysql_fetch_array($get_bank, MYSQL_ASSOC);
                                                            ?>
                                                            <table cellpadding="1" cellspacing="1" align="center">
                                                                <tr><td>Bank</td><td>:</td><td><?= $bank['bank_name'] ?></td></tr>
                                                                <tr><td>Company Name</td><td>:</td><td><?= $bank['acc_name'] ?></td></tr>
                                                                <tr><td>Acc. Number</td><td>:</td><td><?= $bank['acc_no'] ?></td></tr>

                                                            </table><br>
                                                            If payment have been made, kindly click on "<strong>Make payment record</strong>" to continue.<br>
                                                            If you wish to cancel this order, kindly click on "<strong>Cancel this order</strong>".<br>

                                                            <?
                                                        } elseif ($b['status'] == 1) {
                                                            ?>
                                                            <font class="page_description">Your order is currently under verification. Kindly check on the status of your order again in the next 24 hours. You may also have this order cancelled by clicking on "<strong>cancel this order</strong>".</font>


                                                            <?
                                                        } elseif ($b['status'] == 3) {
                                                            ?>
                                                            <font class="page_description">Your payment is currently under verification. Kindly check on the status of your order in the next 48 hours.</font><br>
                                                            <font class="page_description">If you are making several payments for this purchase. Please continue to make the payment record in our system.</font>

                                                            <?
                                                        } elseif ($b['status'] == 4) {
                                                            ?>
                                                            <font class="page_description">Thank you for your payment. A receipt has been sent to your email. Kindly check on the status of your order in the next 48 hours.</font>


                                                            <?
                                                        } elseif ($b['status'] == 5) {
                                                            ?>
                                                            <font class="page_description">Your goods have been delivered. If you have received the goods, kindly click on the "<strong>I have received the goods</strong>" button. 
                                                            <br><u>Delivery information are as follows</u>:
                                                            <table>
                                                                <td>Courier Name</td><td>:</td><td><?= $b['courier_name'] ?></td>
                                                                <tr><td>Reference Number</td><td>:</td><td><?= $b['ref_no'] ?></td></tr>										
                                                                <tr><td>ETA</td><td>:</td><td><?= $b['eta'] ?></td></tr>	
                                                            </table>
                                                            Please note that if you have not received the goods after the date stated above, kindly <a href="os_contactus.php">contact us</a>.
                                                            </font>


                                                            <?
                                                        } elseif ($b['status'] == 7) {
                                                            ?>
                                                            <font color="#0000FF">Sorry, the products you have been ordered is currently out of stock.</font>



                                                            <?
                                                        } elseif ($b['status'] == -3) {
                                                            ?>

                                                            <font class="page_description">Your order is under verifying, please check your email for updates.</font> 

                                                            <?
                                                        } elseif ($b['status'] == -4) {
                                                            ?>

                                                            <font class="page_description">Paid by paypal, please check your email for updates.</font> 

                                                        <? } ?> 

                                                    </fieldset>
                                                </td></table>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td rowspan="3" align="center">

                                            <br>
                                            <?
                                            if ($b['status'] == 2 || $b['status'] == 3) {
                                                ?>
                                                <input type="button" value="Make payment record" class="button" onClick="window.open('os_paymentmade.php?id=<?= $bid ?>','_self')">

                                                <?
                                            }
                                            ?>
                                            <?
                                            if ($b['status'] != -4) {
                                                if ($b['status'] <= 2 || $b['status'] == 7) {
                                                    ?>
                                                    <input type="button" value="Cancel this order" class="button" onClick="window.open('os_cancelbill.php?id=<?= $bid ?>','_self')">

                                                    <?
                                                }
                                            }
                                            ?>

                                            <?
                                            if ($b['status'] == 5) {
                                                ?>
                                                <input type="button" value="I have received the goods" class="button" onClick="window.open('os_receivedgoods.php?id=<?= $bid ?>','_self')">


                                                <?
                                            }
                                            ?>

                                            <br><br>
                                        </td>
                                    </tr>
                                </table>
                                <table width="90%" bgcolor="#CCCCCC" cellpadding="1" cellspacing="1">
                                    <tr class="table_header" align="center">
                                        <td width="42%">Product</td><td width="13%">Unit price(<?= $module_currency_code ?>)</td><td width="15%">Product code</td><td width="11%">Quantity</td><td width="15%">Total(<?= $module_currency_code ?>)</td>
                                    </tr>
                                    <?
                                    $total_item = 0;
                                    $total_piece = 0;
                                    $total_allprice = 0;
                                    $no = 0;
                                    while ($c = mysql_fetch_array($get_item)) {
                                        $total_price = 0;
                                        $get_product = mysql_query("select a.product_name_eng,a.product_code,b.description,b.unit_price from os_products a inner join os_product_inventory b on a.id=b.pid where b.id=" . $c['item_id']);
                                        $a = mysql_fetch_array($get_product, MYSQL_ASSOC);
                                        $total_price = $a['unit_price'] * $c['quantity'];
                                        $total_allprice = $total_allprice + $total_price;
                                        if ($no % 2 == 0) {
                                            $class1 = "table_item_even";
                                            $class2 = "table_item_odd";
                                        } else {
                                            $class1 = "table_item_odd";
                                            $class2 = "table_item_even";
                                        }
                                        ?>
                                        <tr height="30" valign="top" class="<?= $class1 ?>"  onmouseover="this.className='table_item_mouseover'" onmouseout="this.className='<?= $class1 ?>'" align="center">
                                            <td align="left"><a href="os_view_full.php?id=<?= $c['pid'] ?>"><?= $a['product_name_eng'] ?></a>
                                                <br>Product Option : <strong><?= $a['description'] ?></strong></td><td><?= $a['unit_price'] ?></td><td><?= $a['product_code'] ?></td><td><?= $c['quantity'] ?></td>
                                            <td align="right"><?= number_format($total_price, 2) ?></td>
                                        </tr>
                                        <?
                                        $get_addon = mysql_query("select * from os_bill_items where addon_for='" . $c['id'] . "'") or die(mysql_error());

                                        if (mysql_num_rows($get_addon) > 0) {
                                            ?>
                                            <tr height="30" valign="top" class="table_addon_item"  onmouseover="this.className='table_item_mouseover'" onmouseout="this.className='table_addon_item'" >
                                                <td colspan="6" valign="middle">
                                                    <strong> Add-on(s)</strong>
                                                </td>									
                                            </tr>
                                            <?
                                            $no++;
                                            while ($addon = mysql_fetch_assoc($get_addon)) {
                                                $get_product = mysql_query("select a.product_name_eng,a.product_code,b.description,b.unit_price from os_products a inner join os_product_inventory b on a.id=b.pid where b.id=" . $addon['item_id']);
                                                $ap = mysql_fetch_array($get_product, MYSQL_ASSOC);
                                                $total_price = $ap['unit_price'] * $addon['quantity'];
                                                $total_allprice = $total_allprice + $total_price;

                                                if ($no % 2 == 0) {
                                                    $class1 = "table_item_even";
                                                    $class2 = "table_item_odd";
                                                } else {
                                                    $class1 = "table_item_odd";
                                                    $class2 = "table_item_even";
                                                }
                                                ?> 
                                                <tr height="30" valign="top" class="table_addon_item"  onmouseover="this.className='table_item_mouseover'" onmouseout="this.className='table_addon_item'" align="center">
                                                    <td align="left"><a href="os_view_full.php?id=<?= $ap['pid'] ?>"><?= $ap['product_name_eng'] ?></a>
                                                        <br>Product Option : <strong><?= $ap['description'] ?></strong></td><td><?= $ap['unit_price'] ?></td><td><?= $ap['product_code'] ?></td><td><?= $addon['quantity'] ?></td>
                                                    <td align="right"><?= number_format($total_price, 2) ?></td>
                                                </tr>


                                                <?
                                                $no++;
                                            }
                                        }

                                        $no++;
                                    }
                                    ?>
                                </table></td>
                        </tr>

                        <tr>
                            <td><strong>Order Information</strong></td>
                        </tr>
                        <tr>
                            <td>
                                <table width="100%">
                                    <? if ($b['last_update_by_machant'] != '0000-00-00 00:00:00') { ?>
                                        <tr>
                                            <td colspan="4">Last status update by Admin on <?= $b['last_update_by_machant'] ?></td>
                                        </tr>
                                    <? } ?> 
                                    <tr>
                                        <td width="20%">Order number</td><td width="2%">:</td><td width="27%"><?= $b['order_request_number'] ?>	</td>
                                        <td width="18%">Order amount</td><td width="2%">:</td><td width="31%"><?= number_format($b['total_amount'], 2) ?></td>
                                    </tr>
                                    <tr>
                                        <td width="20%">Order date</td><td width="2%">:</td><td width="27%"><?= $b['create_date'] ?></td>
                                        <td width="18%">Shipment fees</td><td width="2%">:</td><td width="31%"><?= number_format($b['transport_fees'], 2) ?></td>
                                    </tr>
                                    <tr>
                                        <td width="20%"></td><td width="2%"></td><td width="27%"></td>
                                        <td width="18%">Discount</td><td width="2%">:</td><td width="31%">-<?= $b['discount'] ?>%</td>
                                    </tr>
                                    <?
                                    $discount = ($b['discount'] / 100) * $b['total_amount'];

                                    $total_amount = $b['total_amount'] - $discount;
                                    $final_amount = $b['transport_fees'] + $total_amount;
                                    ?>

                                    <?
                                    $get_voucher = mysql_query("select * from os_discount_voucher_used where bill_id=$bid") or die(mysql_error());
                                    if (mysql_num_rows($get_voucher) > 0) {

                                        $voucher = mysql_query("select * from os_discount_voucher where id='" . mysql_result($get_voucher, 0, "voucher_id") . "'") or die(mysql_error());

                                        $v = mysql_fetch_array($voucher, MYSQL_ASSOC);

                                        if ($v['discount_type'] == "P") {
                                            $desc = number_format($v['discount_amount'], 0) . "% off";
                                            $discount = ($total_allprice * $v['discount_amount']) / 100;

                                            $final_amount = $final_amount - $discount;
                                        } else {
                                            $desc = " $module_currency_code " . $v['discount_amount'] . " off";
                                            $discount = $v['discount_amount'];
                                            $final_amount = $final_amount - $v['discount_amount'];
                                        }
                                        ?>
                                        <tr>
                                            <td width="20%"></td><td width="2%"></td><td width="27%"></td>
                                            <td><?= $v['voucher_name'] ?>(<?= $v['voucher_code'] ?>) <?= $desc ?> </td><td width="1%">:</td><td> - <?= number_format($discount, 2) ?></td>


                                            <?
                                        }
                                        ?>	


                                    <tr>
                                        <td width="20%"></td><td width="2%"></td><td width="27%"></td>
                                        <td width="18%">Overall amount</td><td width="2%">:</td><td width="31%"><?= number_format($final_amount, 2) ?></td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td><strong>Receiver Information</strong></td>
                        </tr>
                        <tr>
                            <td>
                                <table width="100%">
                                    <tr>
                                        <td width="20%">Name</td><td width="2%">:</td><td width="27%"><?= $b['receiver_name'] ?>
                                        </td>
                                        <td width="18%">Email</td><td width="2%">:</td><td width="31%"><?= $b['email'] ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td width="20%">Address</td><td width="2%">:</td><td width="27%"><?= $b['address1'] ?>
                                        </td>
                                        <td width="18%">Contact number</td><td width="2%">:</td><td width="31%"><?= $b['contact_number'] ?>
                                        </td>
                                    <tr>
                                        <td colspan="2"></td><td width="27%"><?= $b['address2'] ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td width="20%">Postcode</td><td width="2%">:</td><td width="27%"><?= $b['postcode'] ?>
                                        </td>
                                        <td width="18%">City</td><td width="2%">:</td><td width="31%"><?= $b['city'] ?>
                                        </td>
                                    </tr>

                                    <?
                                    if ($b['state'] != 0) {
                                        $get_place = mysql_query("select * from os_state where id=" . $b['state']) or die(mysql_error());
                                        $place = mysql_result($get_place, 0, "name_eng");
                                        $state = "$place";
                                    } else {
                                        $state = "-";
                                    }

                                    if ($b['country'] != 0) {
                                        $get_country = mysql_query("select * from os_country where id=" . $b['country']) or die(mysql_error());
                                        $country = mysql_result($get_country, 0, "engname");
                                        $word_country = "$country";
                                        if (mysql_result($get_country, 0, 'area_type') == "") {
                                            $area_type = "State";
                                        } else {
                                            $area_type = mysql_result($get_country, 0, 'area_type');
                                        }
                                    } else {
                                        $word_country = "Other";
                                        $area_type = "State";
                                    }
                                    ?>
                                    <tr>
                                        <td><?= $area_type ?></td><td>:</td><td><?= $state ?></td>
                                        <td>Country</td><td>:</td><td>
                                            <?= $word_country ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Remark</td><td>:</td><td colspan="4"><?= $b['message'] ?></td>

                                    </tr>

                                </table>
                            </td>
                        </tr>
                    </form>
                    <?
                }
                ?>
            </table>

        </Td>
    </tr>
    <?
    if ($b['cc_status'] == 1) {
        $get_cc = mysql_query("select * from os_ccrecord where bill_id=$bid");

        if (mysql_num_rows($get_cc) > 0) {
            $cc = mysql_fetch_array($get_cc, MYSQL_ASSOC);
            ?>
            <tr>
                <Td colspan="5"><br>
                    <table width="100%">
                        <td colspan="6"><b>Credit Card Payment record</b></td>
                        <tr>
                            <td width="20%">Transaction Id</td><td width="1%">:</td><td width="79%"><?= $cc['tran_id'] ?></td>
                        </tr>
                        <tr>
                            <td>Amount Paid</td><td>:</td><td><?= $cc['currency'] ?> <?= $cc['bill_amount'] ?></td>
                        </tr>
                        <tr>
                            <td>Transaction Date &amp; Time</td><td>:</td><Td><?= $cc['payment_date'] ?></Td>
                        </tr>
                    </table>

                </Td>

            </tr>


        <? }
    } ?>
    <?
    $get_payment = mysql_query("select * from os_payment_record where bid='$bid' order by create_datetime asc") or die(mysql_error());
    if (mysql_num_rows($get_payment) > 0) {
        ?>
        <tr>
            <td>
                <table width="100%" cellpadding="0" cellspacing="0" class="solidborder">
                    <?
                    $no = 1;
                    while ($p = mysql_fetch_array($get_payment)) {

                        if ($p['is_paypal'] == "1") {
                            $ref_no = $p['payer_id'];
                        } else {
                            $ref_no = $p['accountnumber'];
                        }
                        ?>
                        <tr>
                            <td width="100%">
                                <table width="100%" >
                                    <td colspan="6" class="table_header" align="left"><b>Payment record <?= $no ?></b></td>
                                    <tr>
                                        <td width="22%">Payment date</td><td width="2%">:</td><td width="18%"><?= $p['paymentdate'] ?></td>
                                        <td width="20%">Paid amount</td><td width="2%">:</td><td width="36%"><?= $module_currency_code ?> <?= number_format($p['paidamount'], 2) ?></td>
                                    </tr>
                                    <tr>
                                        <td>Bank</td><td>:</td><td><?= $p['paymentbank'] ?></td>
                                        <td>Reference Number</td><td>:</td><td><?= $ref_no ?></td>
                                    </tr>
                                    <tr>
                                        <td >Remark</td><td>:</td ><td colspan="4"><?= $p['message'] ?></td>
                                    </tr>
                                    <tr>

                                    </tr>
                                </table>

                            </td>

                        </tr>
        <? $no++;
    } ?>
                </table>
            </td>
        </tr>

<? } ?>		  
</table>


<? require("required/footer.php"); ?>	